#!/bin/bash


function call_set_config_file_create_false {
    {{{ set_config_file("$1", "Compression", "no", False) | indent(4) }}}
}


function call_set_config_file {
    {{{ set_config_file("$1", "Compression", "no", True) | indent(4) }}}
}

function call_set_config_file_rsyslog {
	{{{ set_config_file(path="$1",
                    parameter="\$DefaultNetstreamDriver", value="gtls", create=true, separator=" ", separator_regex=" ") }}}
}

@test "Basic value remediation" {
    tmp_file="$(mktemp)"
    printf "%s\n" "Compression yes" > "$tmp_file"
    expected_output="Compression no\n"

    call_set_config_file "$tmp_file"

    run diff "$tmp_file" <(printf "$expected_output")
    [ "$status" -eq 0 ]

    rm "$tmp_file"
}

@test "No remediation happened" {
    tmp_file="$(mktemp)"
    printf "%s\n" "Compression no" > "$tmp_file"
    expected_output="Compression no\n"

    call_set_config_file "$tmp_file"

    run diff "$tmp_file" <(printf "$expected_output")
    [ "$status" -eq 0 ]

    rm "$tmp_file"
}

@test "Multiline file remediation" {
    tmp_file="$(mktemp)"
    printf "%s\n" "Protocol 2" "Compression yes" "Port 22" > "$tmp_file"
    expected_output="Protocol 2\nPort 22\nCompression no\n"

    call_set_config_file "$tmp_file"

    run diff "$tmp_file" <(printf "$expected_output")
    [ "$status" -eq 0 ]

    rm "$tmp_file"
}

@test "No remediation on commented line" {
    tmp_file="$(mktemp)"
    printf "%s\n" "Protocol 2" "# Compression yes" "Port 22" > "$tmp_file"
    expected_output="Protocol 2\n# Compression yes\nPort 22\nCompression no\n"

    call_set_config_file "$tmp_file"

    run diff "$tmp_file" <(printf "$expected_output")
    [ "$status" -eq 0 ]

    rm "$tmp_file"
}

@test "Create if missing" {
    tmp_file="$(mktemp)"
    printf "%s\n" "Protocol 2" "Port 22" > "$tmp_file"
    expected_output="Protocol 2\nPort 22\nCompression no\n"

    call_set_config_file "$tmp_file"

    run diff "$tmp_file" <(printf "$expected_output")
    [ "$status" -eq 0 ]

    rm "$tmp_file"
}

@test "Create file if doesn't exist" {
    tmp_file="$(mktemp)"
    rm "$tmp_file"
    expected_output="Compression no\n"

    call_set_config_file "$tmp_file"

    run diff "$tmp_file" <(printf "$expected_output")
    [ "$status" -eq 0 ]

    rm "$tmp_file"
}

@test "Do not create if missing" {
    tmp_file="$(mktemp)"
    rm "$tmp_file"

    run call_set_config_file_create_false "$tmp_file"

    [ "$status" -eq 1 ]
    [[ "$output" =~ "Path '$tmp_file' wasn't found" ]]
}


@test "Case insensitive remediation" {
    tmp_file="$(mktemp)"
    printf "%s\n" "Protocol 2" "COMPRESSION YES" "Port 22" > "$tmp_file"
    expected_output="Protocol 2\nPort 22\nCompression no\n"

    call_set_config_file "$tmp_file"

    run diff "$tmp_file" <(printf "$expected_output")
    [ "$status" -eq 0 ]

    rm "$tmp_file"
}

@test "Case sensitive remediation" {
    tmp_file="$(mktemp)"
    printf "%s\n" "Protocol 2" "COMPRESSION YES" "Port 22" > "$tmp_file"
    expected_output="Protocol 2\nCOMPRESSION YES\nPort 22\nCompression no\n"

    {{{ set_config_file("$tmp_file", "Compression", "no", True, "", "", False) | indent(4) }}}

    run diff -U2 "$tmp_file" <(printf "$expected_output")
    echo "$output"
    [ "$status" -eq 0 ]

    rm "$tmp_file"
}

@test "Basic Bash remediation" {
    tmp_file="$(mktemp)"
    printf "%s\n" "something=foo" > "$tmp_file"
    expected_output="something='va lue'\n"

    {{{ bash_shell_file_set("$tmp_file", "something", "va lue") | indent(4) }}}

    run diff -U2 "$tmp_file" <(printf "$expected_output")
    echo "$output"
    [ "$status" -eq 0 ]

    rm "$tmp_file"
}

@test "Variable remediation - preserve dollar and use double quotes" {
    tmp_file="$(mktemp)"
    printf "%s\n" "something=bar" > "$tmp_file"
    expected_output='something="$value"'"\n"

    {{{ bash_shell_file_set("$tmp_file", "something", '$value') | indent(4) }}}

    run diff -U2 "$tmp_file" <(printf "$expected_output")
    echo "$output"
    [ "$status" -eq 0 ]

    rm "$tmp_file"
}

@test "Basic Bash remediation - don't quote" {
    tmp_file="$(mktemp)"
    printf "%s\n" "something=foo" > "$tmp_file"
    expected_output="something=va lue\n"

    {{{ bash_shell_file_set("$tmp_file", "something", "va lue", no_quotes=true) | indent(4) }}}

    run diff -U2 "$tmp_file" <(printf "$expected_output")
    echo "$output"
    [ "$status" -eq 0 ]

    rm "$tmp_file"
}

@test "Variable remediation - don't quote" {
    tmp_file="$(mktemp)"
    printf "%s\n" "something=bar" > "$tmp_file"
    expected_output='something=$value'"\n"

    {{{ bash_shell_file_set("$tmp_file", "something", '$value', no_quotes=true) | indent(4) }}}

    run diff -U2 "$tmp_file" <(printf "$expected_output")
    echo "$output"
    [ "$status" -eq 0 ]

    rm "$tmp_file"
}

@test "set_config_file - handle escaped dollar" {
    tmp_file="$(mktemp)"
    printf "%s\n" '$DefaultNetstreamDriver bad' > "$tmp_file"
    expected_output='$DefaultNetstreamDriver gtls'"\n"

    call_set_config_file_rsyslog "$tmp_file"

    run diff -U2 "$tmp_file" <(printf "$expected_output")
    echo "$output"
    [ "$status" -eq 0 ]

    rm "$tmp_file"
}
